$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true,
			labelPosition:'left',
			queryIng : false, //正在查询
			queryData:{activeFlag:'1',page:1,rows:10}, //查询条件
			datagrid:[], //数据列表数据
			totalNum:0, //分页控件配置
			data:[],
			defaultCheckedKeys:[],
			addData:{},
			selectedRow:null,
			isEdit:false,
			isEdit2:false,
			rules:{
				name: [{ required: true, message: '该项必填', trigger: 'change' }],
				amountStart: [{ required: true, message: '该项必填', trigger: 'change' }],
				amountEnd: [{ required: true, message: '该项必填', trigger: 'change' }],
				freightExpense: [{ required: true, message: '该项必填', trigger: 'change' }],
			},
			dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
				,detailListVisible:false
				,detailDataVisible:false
			},
			size:"mini",
			detailList:[],
			currentFreight:null,
			detailData:{},
			shopId:$("#shopId").val(),
			shopName:$("#shopName").val(),
		},
		methods: {
			init:function () {
				var vm = this
				vm.query();
			},
			search:function(){
				//点击查询按钮，默认查询第一页。
				this.queryData.page=1;
				this.query();
			},
			setDetail:function(r){
				this.dialogConfig.detailListVisible = true;
				this.currentFreight = r;
				$.ajax({
					url :staticURL+'/mall/freightDetail/listAll',
					data : {
						freightId:r.id,
					},
					dataType : 'json',
					success : function(res) {
						app.detailList = res.data;
					}
				});
			},
			handleAddDetail:function(){
				this.dialogConfig.detailDataVisible = true;
				this.detailData={};
				this.detailData.freightId = this.currentFreight.id;
				this.$refs['detailForm'].resetFields();
				this.isEdit2 = false;
			},
			handleUpdateDetail:function(row){
				this.detailData = JSON.parse(JSON.stringify(row));
				this.dialogConfig.detailDataVisible = true;
				this.isEdit2 = true;
			},
			handleDelDetail:function(row){
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
			        		url : staticURL+"/mall/freightDetail/delete?ids="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.setDetail(app.currentFreight);
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				return;
			},
			query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				vm.queryData.shopId = vm.shopId;
				$.ajax({
					url :staticURL+'/mall/freight/datagrid',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			},
			addItem:function(){
				app.openDialog();
			},
			deleteItem:function(row){
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
			        		url : staticURL+"/mall/freight/delete?ids="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				return;
				
			},
			editItem:function(row){
				app.openDialog(row);
			},
			handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			},
			handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			},
			handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			},
			resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    },
			submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						//验证通过
						$.ajax({
							url : app.saveUrl,
							data : app.addData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.dialogFormVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			},
			submitForm2:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						if(app.detailData.amountStart> app.detailData.amountEnd){
							app.$notify({showClose: true,
						          message: "【起始金额】不可大于【截止金额】",
						          type: 'error',
						          position: 'bottom-right'});
							return;
						}
						//验证通过
						$.ajax({
							url : app.saveUrl2,
							data : app.detailData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.detailDataVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.setDetail(app.currentFreight);
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			},
			openDialog:function(param){
				if(param){
					app.isEdit = true;
					//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
					//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
					//app.addDatat同时被reset,会导致bug
					app.addData = JSON.parse(JSON.stringify(param)); 
					
				}else{
					app.isEdit = false;
					app.addData = {
							orderNum:1,
							shopId:app.shopId
							};
				}
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
				
			},
			closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
				app.resetForm('dialogForm')
			},
			closeDialog2:function(){
				app.dialogConfig.detailDataVisible=false;
				app.resetForm('detailForm')
			},
			closeDialog3:function(){
				app.dialogConfig.detailListVisible=false;
			},
		    datagridSelect:function(row, event, column){
				app.selectedRow = row;
			}
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/mall/freight/edit'
				}else{
					return staticURL+'/mall/freight/add'
				}
			},
			saveUrl2:function(){
				if(app.isEdit2){
					return staticURL+'/mall/freightDetail/edit'
				}else{
					return staticURL+'/mall/freightDetail/add'
				}
			},
			columnWidth:function(){

				let w = $(window).width();   

				w = w - 48-48-50-150;
				
				return Math.floor(w/5);
			}
		},
		created: function () {
			this.init();
		},
		updated:function(){
		}
	});
});